﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _00.@string
{
    class Program
    {
        //还有StringBuilder类
        static void Main(string[] args)
        {

            {//0. 构造
                string s1 = "abc-def-ghi";
                string s2 = new string('a', 8);
                string s3 = new string(new char[] { 'a', 'b', 'c' }); //char[]
                Console.WriteLine("s1: {0}", s1);
                Console.WriteLine("s2: {0}", s2);
                Console.WriteLine("s3: {0}", s3);
                Console.WriteLine("----------");
            }

            {//1. format
                string s = string.Format("{0} love {1}!", "John", "Nancy");
                Console.WriteLine(s);

                char[] arr = s.ToCharArray();
                Console.WriteLine(arr.Length);
                foreach (var c in arr)
                {
                    Console.Write(c);//
                    Console.Write(' ');//
                }
                Console.WriteLine();
                Console.WriteLine("----------");
            }

            {//2. indexOf/contains /index /subString
                string s1 = "i love you!";
                int i1 = s1.IndexOf('o'); //lastIndexOf
                int i2 = s1.IndexOfAny(new char[] { 'd', 'e', 'f' });
                Console.WriteLine("index: {0} is {1}", i1, s1[i1]); //index
                Console.WriteLine("index: {0} is {1}", i2, s1[i2]);

                bool b1 = s1.Contains('a');
                bool b2 = s1.Contains("def");
                Console.WriteLine(b1);
                Console.WriteLine(b2);

                string s2 = s1.Substring(1);
                string s3 = s1.Substring(1, 3);
                Console.WriteLine("substring(1):{0}", s2);
                Console.WriteLine("substring(1,3):{0}", s3);

                //看把你牛逼的
                Console.WriteLine(new string(s1.Where((c) => c == 'o' || c == 'e').ToArray()));//有意思的语法，跟Java8的lambda类似

                Console.WriteLine("----------");
            }

            {//3. == equals 
                string s1 = "abc";
                string s2 = "abc";
                bool b1 = s1 == s2;
                Console.WriteLine("==: " + b1);

                bool b2 = s1.Equals(s2);
                bool b3 = string.Equals(s1, s2);
                Console.WriteLine("equals: " + b2);
                Console.WriteLine("string.equals: " + b3);
                Console.WriteLine("----------");
            }

            {//4. remove/insert
                string s1 = "0123456789";
                string s2 = s1.Remove(6); //类似go的s[:6]
                string s3 = s1.Remove(4, 5);//从角标4开始，移除5个
                string s4 = s1.Insert(1, "hhh");//角标1插入
                Console.WriteLine(s1);
                Console.WriteLine(s2);
                Console.WriteLine(s3);
                Console.WriteLine(s4);
                Console.WriteLine("----------");
            }
            {//5. Max/Min/First/Last
                string s1 = "0123456789";
                Console.WriteLine("max:{0}, min{1},average-none", s1.Max(), s1.Min());//, s1.Average());
                Console.WriteLine("----------");
            }

            {//6. Length/Count
                string s = "0123456789";
                Console.WriteLine(s.Length);
                Console.WriteLine(s.Count());
                Console.WriteLine("----------");
            }
            {//7. endWith/startWith
                string s = "abcdef";
                bool b1 = s.EndsWith("EF");
                bool b2 = s.EndsWith("EF", StringComparison.OrdinalIgnoreCase);
                bool b3 = s.StartsWith("abc");
                bool b4 = s.StartsWith("aBc", true, null);
                Console.WriteLine(b1);
                Console.WriteLine(b2);
                Console.WriteLine(b3);
                Console.WriteLine(b4);
                Console.WriteLine("----------");
            }

            {//8. trim
                string s = "\t  0123 456  789 \t";
                string s1 = s.Trim();
                Console.WriteLine("s1:[{0}]", s1);
                Console.WriteLine("----------");
            }
            {//9. pad 
                string s = "abc";
                string s1 = s.PadLeft(10);
                string s2 = s.PadLeft(10, '-');
                string s3 = s.PadRight(10, '*');
                Console.WriteLine(s1);
                Console.WriteLine(s2);
                Console.WriteLine(s3);
                Console.WriteLine("----------");
            }


            Console.ReadKey();
        }
    }
}
